<template>
  <div :class="className" :id="id" :style="{height:height,width:width,top:top+'px'}"  />
</template>

<script>
import echarts from 'echarts'

export default {
  props: {
    option: {
      type: Object
    },
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '201px'
    },
    top: {
      default: '0'
    },
    height: {
      type: String,
      default: '200px'
    },
    initCharts: Function
  },
  data() {
    return {
      chart: null
    }
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  mounted() {
    this.initChart(this.option)
  },
  methods: {
    initChart(option) {
      this.chart = echarts.init(document.getElementById(this.id))
      if (option) {
        this.chart.setOption(option)
      } else {
        this.chart.setOption(this.option)
      }
    },
    destroy() {
      if (this.chart) {
        this.chart.dispose()
      }
    }
  }
}
</script>
